#ifndef __clib_tmem_h__
#define __clib_tmem_h__

#include <mem.h>
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

void tmem_base()
{
    int i;
    void *mem;
    pool_init();
    mem = pool_alloc( 256 );
    for( i = 0; i < 2; ++i )
        mem = pool_realloc( mem, rand() );
    pool_free( mem );
}

void tmem( int rnd, int use_sys )
{
    int i;
    int r;
    int t;
    void **pool;
    void* (*pget)(uint32);
    void (*prelease)(void*);
    void* (*preget)(void*,uint32);
    if( use_sys )
    {
        pget = malloc;
        prelease = free;
        preget = realloc;
    }
    else
    {
        pool_init();
        pget = pool_alloc;
        prelease = pool_free;
        preget = pool_realloc;
    }

    srand( GetTickCount() );
    t = GetTickCount();
    pool = (void**)pget( sizeof(void*) * rnd );
    for( i = 0; i < rnd; ++i )
    {
        pool[i] = pget( 256 );
        memset( pool[i], 0, 256 );
    }
    for( i = 0; i < rnd; ++i )
    {
        r = rand();
        r *= rand();
        r %= rnd;
        if( pool[i] )
        {
            prelease( pool[i] );
            pool[i] = NULL;
        }
    }
    for( i = 0; i < rnd; ++i )
    {
        if( pool[i] )
        {
            prelease( pool[i] );
            pool[i] = NULL;
        }
    }
    prelease( pool );
    printf( "use time: %d\n", GetTickCount()-t );
}

#endif // __clib_tmem_h__
